package com.dries.security.validate.code;

import com.dries.common.api.CommonResult;
import com.dries.security.properties.SecurityConstants;
import com.dries.security.validate.code.captcha.SysCaptcha;
import com.dries.security.validate.code.captcha.ValidateCodeProcessorHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description:
 * @Author ZSY
 * @createTime 2020/9/7 16:01
 */
@RestController
public class ValidateCodeController {

    @Resource
    private HttpServletRequest request;
    @Resource
    private HttpServletResponse response;
    @Resource
    private ValidateCodeProcessorHolder validateCodeProcessorHolder;

    /**
     * 创建验证码，根据验证码类型不同，调用不同的 {@link SysCaptcha}接口实现
     * @param type
     */
    @GetMapping(SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/{type}")
    public CommonResult createCode(@PathVariable String type) throws Exception {
        SysCaptcha captcha = validateCodeProcessorHolder.findValidateCodeProcessor(type)
                .render(new ServletWebRequest(request, response));
        return CommonResult.success(captcha);
    }

}
